from time import sleep

from selenium.webdriver.common.by import By

from Common.Utils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class PersonList:
    url=""
    driver=""
    old_count = ""
    def __init__(self):
        self.url="http://127.0.0.1:58080/index.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #检查元素
    def List(self):
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_profile").click()
        BlogDriver.getScreenshort()
        elements_to_check = [
            ("#profile_nickname", "小明"),
            ("#profile_remark", "你们好，我是叫小明！"),
            ("#profile_email", "123@qq.com"),

        ]

        # 循环检查所有元素
        for selector, expected_text in elements_to_check:
            try:
                element = WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR, selector))
                )
                actual_text = element.text
                print(f"验证元素 [{selector}] -> 实际内容: {actual_text}")

                # 断言并附带清晰错误信息
                assert actual_text == expected_text, \
                    f"文本不匹配！期望: '{expected_text}'，实际: '{actual_text}'"

            except AssertionError as e:
                print(f"\u274C 断言失败: {str(e)}")
                # 这里可以添加失败截图等操作
            except Exception as e:
                print(f"\u26A0\uFE0F 发生意外错误: {str(e)}")

    #检查博客数量
    def PostCount(self):
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_profile").click()
        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#profile_articleCount"
        ).text
        old_count = int(old_count)

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#profile_article_body li .text-truncate a strong")]

        count = len(post_titles)
        print(f"数量正确，数量为{count}")
        assert count == old_count, f"数量不符合, 数量为 {old_count}, 实际为 {count}"
